<template>
  <div>
    <el-checkbox-group v-model="checkListone">
      <el-checkbox
        v-for="(item, index) in rolePageList"
        :key="index"
        :label="item.roleName"
      />
    </el-checkbox-group>
  </div>
</template>
<script>
import userManage from "@/api/system-settings/user-manage";
import $baseUrl from "@/utils/baseURL";
import store from "@/store";
import axios from "axios";

import { getToken } from "@/utils/auth";
export default {
  props: {
    username: {
      type: String,
      default: "",
    },
    userstu: {
      type: String,
      default: "",
    },
  },
  data() {
    return {
      rolePageList: [],
      checkListone: [],
      hasrolelist: [],
      hasidlist: [],
    };
  },
  watch: {
    userstu: {
      handler(Val) {
        if (Val === "add") {
          this.checkchange();
        }
      },
    },
  },
  mounted() {
    this.getdatamet();
  },
  methods: {
    getdatamet() {
      userManage.getcheckboxdata().then((res) => {
        this.rolePageList = res.data.content;
        // console.log(res.data)
        this.getrollehas();
      });
      console.log("获取数据");
    },
    getrollehas() {
      userManage.getrollehas(this.username).then((res) => {
        this.hasrolelist = res.data || [];
        let that = this;
        if (this.hasrolelist) {
          this.hasrolelist.forEach((item) => {
            this.rolePageList.forEach((itemtwo, indextwo) => {
              if (itemtwo.roleNo === item.roleNo) {
                that.hasidlist.push(item.roleNo);
                that.checkListone.push(item.roleName);
              }
            });
          });
        }
        // console.log(res.data)
      });
    },
    checkchange() {
      let that = this;
      that.hasidlist = [];
      if (this.checkListone.length > 0) {
        this.checkListone.forEach((item) => {
          this.rolePageList.forEach((itemtwo, indextwo) => {
            if (itemtwo.roleName === item) {
              that.hasidlist.push(itemtwo.roleNo);
            }
          });
        });
      }
      console.log(that.hasidlist, this.checkListone, this.username);
      let params = "";
      if (this.hasidlist.length > 0) {
        params = `roleNo=${this.hasidlist}&username=${this.username}`;
        // params = {
        //   roleNo: eval(this.hasidlist.join(",")),
        //   username:this.username.trim() ,
        // };
      }
      console.log(params);
      let zhurl =
        $baseUrl.agricultureServiceUrl + "v3/authority/v1/role/bindUser";
      params = `roleNo=${this.hasidlist}&username=${this.username.trim()}`;
      axios({
        method: "POST",
        url: zhurl,
        headers: {
          databaseName: sessionStorage.getItem("databaseName"),
          accessToken:
            store.getters.token ||
            sessionStorage.getItem("token") ||
            getToken(),
          organizationList: sessionStorage.getItem("organizationNoList"),
          organizationNo: sessionStorage.getItem("organizationNo"),
          Virtualsystemno: sessionStorage.getItem("Virtualsystemno"),
        },
        data: params,
      }).then((res) => {

        if (res.data.code === 1) {
          this.$emit("users", "");
          this.$message.success("分配角色成功");
        } else {
          this.$message.error(`${res.msg}`);
        }
      });

      // userManage.addroleuth(params).then((res) => {
      //   if (res.code === 1) {
      //     this.$emit("users", "");
      //     this.$message.success("分配角色成功");
      //   } else {
      //     this.$message.error(`${res.msg}`);
      //   }
      // });
    },
  },
};
</script>
